package com.cyanite.playlv2api.parsers.json;

import com.cyanite.playlv2api.types.Tags;
import com.cyanite.playlv2api.types.Venue;
import com.cyanite.playlv2api.types.VenueCategory;

import org.json.JSONException;
import org.json.JSONObject;

public class VenueParser extends AbstractParser<Venue> {

    @SuppressWarnings("unchecked")
    @Override
    public Venue parse(JSONObject json) throws JSONException {

        Venue obj = new Venue();
        if (json.has("vid")) {
            obj.setId(json.getString("vid"));
        }
        if (json.has("vname")) {
            obj.setName(json.getString("vname"));
        }
        if (json.has("addr")) {
            obj.setAddress(json.getString("addr"));
        }
        if (json.has("online")) {
            obj.setCheckInsCountMoment(json.getInt("online"));
        }
        if (json.has("checkins")) {
            obj.setCheckins(
                new GroupParser(
                    new CheckinParser()).parse(json.getJSONArray("checkins")));
        }
        if (json.has("users")) {
            obj.setCheckins(
                new GroupParser(
                    new CheckinParser()).parse(json.getJSONArray("users")));
        }
        if (json.has("city")) {
            obj.setCity(json.getString("city"));
        }
        if (json.has("cityid")) {
            obj.setCityid(json.getString("cityid"));
        }
        if (json.has("street")) {
            obj.setCrossstreet(json.getString("street"));
        }
        if (json.has("distance")) {
            obj.setDistance(String.valueOf(json.getInt("distance")));
        }
        if (json.has("lat")) {
            obj.setGeolat(json.getString("lat"));
        }
        if (json.has("lng")) {
            obj.setGeolong(json.getString("lng"));
        }
        if (json.has("hasTodo")) {
        	obj.setHasTodo(json.getBoolean("hasTodo"));
        }
        if (json.has("phone")) {
            obj.setPhone(json.getString("phone"));
        }
        if (json.has("type")) {
            VenueCategory vc = new VenueCategory();
             vc.setId(json.getInt("type")-1);
             obj.setCategory(vc);
        }
        if (json.has("specials")) {
            obj.setSpecials(
                new GroupParser(
                    new SpecialParser()).parse(json.getJSONArray("specials")));
        }
        if (json.has("country")) {
            obj.setState(json.getString("country"));
        }
        if (json.has("tags")) {
            obj.setTags(
                new Tags(StringArrayParser.parse(json.getJSONArray("tags"))));
        }
        if (json.has("tips")) {
            obj.setTips(
                new GroupParser(
                    new TipParser()).parse(json.getJSONArray("tips")));
        }
        if (json.has("zip")) {
            obj.setZip(json.getString("zip"));
        }

        return obj;
    }
}